<template>
	<view>
		<tn-nav-bar :isBack='true' :backTitle='" "' bottomShadow :alpha="alpha">产品管理</tn-nav-bar>
		<image src="/static/product/topbg.png" class="topbg" mode=""></image>
		<view :style="{paddingTop: vuex_custom_bar_height + 'px'}" class="con">
			<view class="top">
				<image src="/static/product/vipbg.png" class="top__bg" mode=""></image>
				<view class="top__con">
					<view class="top__con__title">
						电子卡管理
					</view>
					<view class="top__con__status">
						已开通
					</view>
					<tn-button fontColor="#47CA7F" shape='round' :fontSize='25'  @click="to('/pages/product/card')">查看详情</tn-button>
				</view>
			</view>
			<view class="tabs">
				<tn-tabs :list="list" :current="current" itemWidth='50%' activeColor="#1E1E1E" inactiveColor="#868A8B"
					:barWidth='32' :barHeight='11' :barStyle="{background:'#45C47B',marginTop:'10rpx'}" :height='80' @change="change"
					:isScroll='false' :fontSize="28" :bold='true'></tn-tabs>
			</view>
			<loading v-if="loading"></loading>
			<view class="list" v-else>
				<view class="list__item" v-for="(item,index) in cardlist" :key="index">
					<view class="list__item__no">
						订单编号: {{item.order_sn}}
					</view>
					<view class="list__item__mid">
						<view class="tn-flex" style="width: 100%;">
							<image :src="item.imgs?URL(item.imgs[0]):'/static/logo.png'" class="list__item__pic" mode="aspectFill"></image>
							<view class="list__item__center">
								<view class="list__item__name">
									{{item.name}}
								</view>
								<view class="list__item__addr tn-text-ellipsis" v-if="item.address">
									{{item.address.province+item.address.city+item.address.area+item.address.detail}}
								</view>
								<view class="list__item__num">
									数量 <image src="/static/product/cha.png" class="cha" mode=""></image> {{item.num?item.num:1}}
								</view>
							</view>
						</view>
						<view class="list__item__price">
							￥{{item.price}}
						</view>
					</view>
					<view class="list__item__bottom">
						{{timeFormat(item.createtime,'yyyy.mm.dd hh:MM')}}
						
						<tn-button @click="to('/pages/product/cardlist?order_sn='+item.order_sn)" v-if="item.pay_state==2" fontColor="#FFFFFF" shape='round' width='158rpx' height='64rpx' :fontSize='25' backgroundColor="tn-bg-lv1">电子卡</tn-button>
						<tn-button  v-if="item.pay_state==2" @click="to('/pages/product/orderdetails?id='+item.id)" fontColor="#FFFFFF" shape='round' height='64rpx' :fontSize='25' backgroundColor="tn-bg-lv1">查看详情</tn-button>
						<tn-button v-if="item.pay_state==1" fontColor="#FFFFFF" shape='round' width='158rpx' height='64rpx' :fontSize='25' backgroundColor="tn-bg-lv2" @click="gopay(item)">去支付</tn-button>
					</view>
				</view>
				<!-- <view class="list__item" v-for="(item,index) in orderlist" :key="index">
					<view class="list__item__no">
						订单编号: {{item.order_sn}}
					</view>
					<view class="list__item__mid">
						<view class="tn-flex" style="width: 100%;">
							<image :src="item.imgs?URL(item.imgs[0]):'/static/logo.png'" class="list__item__pic" mode="aspectFill"></image>
							<view class="list__item__center">
								<view class="list__item__name">
									{{item.name}}
								</view>
								<view class="list__item__addr tn-text-ellipsis" v-if="item.address">
									{{item.address.province+item.address.city+item.address.area+item.address.detail}}
								</view>
								<view class="list__item__num">
									数量 <image src="/static/product/cha.png" class="cha" mode=""></image> {{item.num}}
								</view>
							</view>
						</view>
						<view class="list__item__price">
							￥{{item.price}}
						</view>
					</view>
					<view class="list__item__bottom">
						{{timeFormat(item.createtime,'yyyy.mm.dd hh:MM')}}
						<tn-button v-if="item.pay_state==2&&item.status==2" fontColor="#FFFFFF" shape='round' width='158rpx' height='64rpx' :fontSize='25' backgroundColor="tn-bg-lv">确认完成</tn-button>
						<tn-button v-if="item.pay_state==2&&item.status==3" fontColor="#FFFFFF" shape='round' width='158rpx' height='64rpx' :fontSize='25' backgroundColor="tn-bg-lv1">已完成</tn-button>
						<tn-button v-if="item.pay_state==1" fontColor="#FFFFFF" shape='round' width='158rpx' height='64rpx' :fontSize='25' backgroundColor="tn-bg-lv2">去支付</tn-button>
					</view>
				</view> -->
			</view>
		</view>


	</view>
</template>

<script>
	import user from '@/api/_user.js'
	export default {
		data() {
			return {
				list: [{
					name: '已支付',
					pay_state:2
				}, {
					name: '未支付',
					pay_state:1
				}],
				current: 0,
				alpha:true,
				orderlist:[],
				cardlist:[],
				status:2,
				loading:true
			};
		},
		methods: {
			change(index) {
				this.current = index;
				this.status=this.list[index].pay_state
				this.getlist()
			},
			getlist(){
				this.loading=true
				user.myorder({type:3,pay_state:this.status}).then(res=>{
					if(res.data.code==1){
						res.data.data.map(t=>{
							if(t.image){
								t.imgs=t.image.split(',').filter(Boolean)
							}
							
						})
						this.cardlist=res.data.data
						this.loading=false
					}
				})
			},
			gopay(item){
				user.reloadPay({id:item.id}).then(res=>{
					if(res.data.code==1){
						this.paybuy(res)
					}
				})
			},
			paybuy(res) {
				let _this = this
				let data = res.data.data
				let appId = data.appId;
				let timeStamp = data.timeStamp;
				let nonceStr = data.nonceStr;
				let packageX = data.package;
				let signType = 'MD5';
				let paySign = data.paySign;
			
				uni.requestPayment({
					provider: 'wxpay',
					appId: appId, //小程序Appid
					timeStamp: timeStamp, //创建订单时间戳
					nonceStr: nonceStr,
					package: packageX, // 订单包
					signType: signType, // 加密方式统一'MD5'
					paySign: paySign, // 后台支付签名返回
					success: function(res) {
						_this.$tn.message.toast('支付成功')
						setTimeout(() => {
							_this.change(0)
						}, 1000)
					},
					fail: function(err) {
						_this.$tn.message.toast('支付失败')
					},
					complete() {
			
					}
				});
			
			},
		},
		onLoad() {
			this.getlist()
		}
	}
</script>

<style lang="scss">
	page {
		background-color: #F4F4F4;
	}

	

	.con {
		position: relative;
		padding: 30rpx;

		.top {
			width: 690rpx;
			height: 258rpx;
			position: relative;
			margin-top: 30rpx;

			&__bg {
				width: 690rpx;
				height: 258rpx;
				position: absolute;
				top: 0;
				left: 0;
			}

			&__con {
				position: absolute;
				padding: 54rpx 0 32rpx 60rpx;
				width: 100%;
				&__title {
					font-family: PingFang SC;
					font-weight: bold;
					font-size: 30rpx;
					color: #FFFFFF;
					line-height: 30rpx;
				}

				&__status {
					font-family: PingFang SC;
					font-weight: 500;
					font-size: 24rpx;
					color: #FFFFFF;
					line-height: 24rpx;
					margin-top: 19rpx;
					margin-bottom: 39rpx;
				}
			}
		}
		.tabs{
			padding: 10rpx 0;
		}
		.list{
			&__item{
				padding: 40rpx 24rpx 28rpx 30rpx;
				background: #FFFFFF;
				border-radius: 30rpx;
				margin-bottom: 26rpx;
				&__no{
					font-family: PingFang SC;
					font-weight: 500;
					font-size: 26rpx;
					color: #606060;
					line-height: 26rpx;
					margin-bottom: 40rpx;
				}
				&__mid{
					display: flex;
					align-items: center;
					// justify-content: space-between;
					
				}
				&__pic{
					width: 139rpx;
					height: 139rpx;
					border-radius: 10rpx;
				}
				&__center{
					height: 130rpx;
					display: flex;
					flex-direction: column;
					justify-content: space-between;
					margin-left: 26rpx;
					width:336rpx;
					padding: 3rpx 0 6rpx;
				}
				&__name{
					font-family: PingFang SC;
					font-weight: 800;
					font-size: 32rpx;
					color: #1E1E1E;
					line-height: 32rpx;
				}
				&__addr{
					font-family: PingFang SC;
					font-weight: 500;
					font-size: 24rpx;
					color: #6A6A6A;
					
				}
				&__num{
					font-family: PingFang SC;
					font-weight: 500;
					font-size: 24rpx;
					color: #6A6A6A;
					display: flex;
					align-items: center;
				}
				&__price{
					font-family: DINNextLTPro-Bold;
					font-weight: bold;
					font-size: 38rpx;
					color: #FD2A00;
					line-height: 38rpx;
					align-self: flex-end;
				}
				&__bottom{
					font-family: PingFang SC;
					font-weight: 500;
					font-size: 24rpx;
					color: #6A6A6A;
					line-height: 24rpx;
					justify-content: space-between;
					display: flex;
					align-items: center;
					margin-top: 30rpx;
				}
			}
		}
	}
	.cha{
		width: 16rpx;
		height: 16rpx;
		margin: 0 6rpx;
	}
</style>